﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class RetrieveAccountPlanningByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveAccountPlanningByCifRequest, RetrieveAccountPlanningByCifResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveAccountPlanningByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveAccountPlanningByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
        }

        protected override bool OnExecute(RetrieveAccountPlanningByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AccountPlanningRecord> records = new List<AccountPlanningRecord>();
            List<AccountPlanning> listCustomer = new List<AccountPlanning>();
            listCustomer = Providers.Portfolios.RetrieveAccountPlanningByCif(Cif);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new AccountPlanningRecord
                {
                    Cif = x.Cif,
                    CustName = x.CustName,
                    Industry = x.Industry,
                    ActionPlanT62016 = x.ActionPlanT62016,
                    KhHoTro = x.KhHoTro,
                    PaymentMethod = x.PaymentMethod,
                    SpBanCheo = x.SpBanCheo,
                    Plan2016 = x.Plan2016,
                    SpDangSuDung = x.SpDangSuDung,
                    ThuPhi2016 = x.ThuPhi2016.HasValue ? x.ThuPhi2016.Value.ToString("#,###", cul.NumberFormat) : 0.ToString("#,###", cul.NumberFormat),
                    TinhHinhKhaiThac = x.TinhHinhKhaiThac,
                    TyLeSuDungHanMuc = x.TyLeSuDungHanMuc,
                    VuongMac = x.VuongMac,
                    WalletShare = x.WalletShare,
                    MainSector = x.MainSector
                }).ToList();
            }
            response.Records = records;
            return isSuccess;
        }
    }
}
